package ldy.es.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 包名. com.tiens.es.utils
 * Copyright tiens. All Rights Reserved.
 * 描述详情：工具类
 * 创建者：dongya
 * 创建时间：2017/3/29-14:38.
 * <p>
 */
public class Utils {

    private static final Logger logger = LoggerFactory.getLogger(Utils.class);
    /**
     * 根据对象和属性名称反射出属性值
     * @param t 对象类, fieldName 属性名称
     * @return java.lang.Object
     * 创建人：liudy
     * 创建时间：2017/3/29-14:39
     * 修改人：liudy
     * 修改时间：2017/3/29-14:39
     * 修改内容：
     */
    public static  <T> Object  getInvokeMethod(T t, String fieldName) {
        Class clazz = t.getClass();
        if(t instanceof HashMap){
            Map map = (Map) t;
            return map.get(fieldName);
        }
        else if(t instanceof JSONObject){
            JSONObject map = (JSONObject) t;
            return map.get(fieldName);
        }
        else{

            String subFirstLetter = fieldName.substring(0, 1).toUpperCase();
            try{

                Method method = clazz.getDeclaredMethod("get" + subFirstLetter + fieldName.substring(1));
                return String.valueOf(method.invoke(t)) ;
            }
            catch(Exception e){
                logger.info("can not get declard Method " + e);
            }
        }
        return null;
    }

    public static boolean isNotEmpty(String msg){
        return StringUtils.isNotEmpty(msg) && StringUtils.isNotBlank(msg) && !"".equals(msg.trim());
    }
    public static boolean isNotEmpty(Object ... msg){
        return msg != null && msg.length > 0;
    }
    public static boolean isNotEmpty(Integer [] msg){
        return msg != null && msg.length > 0;
    }
    public static boolean isSetEmpty(Set<Object> msg){
        return msg == null || msg.size() == 0;
    }
    public static boolean isNotSetEmpty(Set<Integer> msg){
        return msg != null && msg.size() > 0;
    }
    public static boolean isEmpty(String msg){
        return StringUtils.isEmpty(msg) || StringUtils.isBlank(msg) || "".equals(msg.trim());
    }
    public static boolean isEmpty(String ... msg){
        return msg == null || msg.length == 0;
    }
    public static boolean isEmpty(Map<String, Object> msg){
        return msg == null || msg.size() == 0;
    }
    public static boolean isNotEmpty(JSONArray msg){
        return msg != null && msg.size() > 0;
    }

    public static <T> boolean isEmpty(List<T> list){
        return list == null || list.size() == 0;
    }

    public static <T> boolean isNotEmpty(List<T> list){
        return list!= null && list.size() > 0;
    }

    public static boolean isURL(String url){
        String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]" ;
        Pattern patt = Pattern. compile(regex );
        Matcher matcher = patt.matcher(url);
        return matcher.matches();
    }

    public static String trim(String msg){
        if(msg == null){
            return msg;
        }
        return msg.trim();
    }
    /**
     * 把文件生成字符串
     * @param file File类型
     * @return java.lang.String
     * 创建人：liudy
     * 创建时间：2017/3/29-14:43
     */
    public static String readFiletoStr(File file){
        StringBuffer fileBuffer = new StringBuffer();
        try{
            BufferedReader fr = new BufferedReader(new FileReader(file));
            String line;
            while((line=fr.readLine())!=null){
                fileBuffer.append(line);
            }
            fr.close();
        }
        catch (Exception e){
            logger.error("read file convert to string error:" + e);
        }
        return fileBuffer.toString();
    }
}
